import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/uidata.dart';

import '../../helper/menuHelper.dart';
import '../../helper/routeHelper.dart';

import '../../pages/sdzx/sdzx_helper.dart';

import '../../spaces/spaceX/spacex_list.dart';
import '../../widgets/IconWidget.dart';

import '../../widgets/menu_item.dart';

class WorkRecordPage extends StatefulWidget {
  @override
  WorkRecordPage();

  @override
  WorkRecordPageState createState() => new WorkRecordPageState();
}

class WorkRecordPageState extends State<WorkRecordPage> {
  late Map<int, List<MenuItemViewModel>> mList;
  late List<MenuItemViewModel> mL1;
  late List<MenuItemViewModel> mL2;
  late List<MenuItemViewModel> mL3;
  late List<MenuItemViewModel> mL4;
  late List<MenuItemViewModel> mL5;

  bool ifLoaded = false;
  late String authority;
  @override
  void initState() {
    // TODO: implement initState

    super.initState();
    //  getML();
    LoadData();
  }

  Icon getIcon(IconData d1) {
    return Icon(d1, size: 28.0, color: Colors.white);
  }

  Icon getAlIcon(int x) {
    return getIcon(IconData(x, fontFamily: 'alFonts'));
  }

  void LoadData() async {
    authority = "all";
    getML(this.context);
    //print(sz);
    setState(() {
      ifLoaded = true;
    });

    //ChannelHelper.showToast("更新完成!");
  }

  void getML(BuildContext ctx) {
    print('getML');
    mList = new Map<int, List<MenuItemViewModel>>();
    mList[0] = <MenuItemViewModel>[];
    mList[1] = <MenuItemViewModel>[];
    mList[2] = <MenuItemViewModel>[];
    mList[3] = <MenuItemViewModel>[];

    getML3(ctx);
  }

  void getML3(BuildContext ctx) {
    //addMenu(3, getMenu(getIcon(Icons.view_headline), "收文登记", ()=>RouteHelper.ShowPage(context, MovieListPage(SdzxHelper.projectRecord,true,2))));
    //addMenu(3, getMenu(getIcon(Icons.description), "发文登记",  ()=>RouteHelper.ShowPage(context, MoviePlayPage("5F141544-BEA6-4A3D-A437-5B6D5D8C8517"))));
    //addMenu(3, getMenu(getIcon(Icons.widgets), "项目管理", null));
    addMenu(
        3,
        getMenu(
            getIcon(Icons.person),
            "值班值守",
            () => RouteHelper.ShowPage(
                context,
                SpaceXListPage(SdzxHelper.zhibanRecord, "值班值守",
                    IconWidget.getImage(Colors.blue, Icons.person)))));
    addMenu(
        3,
        getMenu(
            getIcon(Icons.remove_red_eye),
            "考察调研",
            () => RouteHelper.ShowPage(
                context,
                SpaceXListPage(SdzxHelper.researchRecord, "考察调研",
                    IconWidget.getImage(Colors.blue, Icons.remove_red_eye)))));
    addMenu(
        3,
        getMenu(
            getIcon(Icons.book_rounded),
            "会议记录",
            () => RouteHelper.ShowPage(
                context,
                SpaceXListPage(SdzxHelper.meetRecord, "会议记录",
                    IconWidget.getImage(Colors.blue, Icons.book_rounded)))));
    addMenu(
        3,
        getMenu(
            getIcon(Icons.flag),
            "党建活动",
            () => RouteHelper.ShowPage(
                context,
                SpaceXListPage(SdzxHelper.flagRecord, "党建活动",
                    IconWidget.getImage(Colors.blue, Icons.flag)))));
    addMenu(
        3,
        getMenu(
            getIcon(Icons.nature),
            "洪水记录",
            () => RouteHelper.ShowPage(
                context,
                SpaceXListPage(SdzxHelper.floodRecord, "洪水记录",
                    IconWidget.getImage(Colors.blue, Icons.nature)))));
    addMenu(
        3,
        getMenu(
            getIcon(Icons.category),
            "工会活动",
            () => RouteHelper.ShowPage(
                context,
                SpaceXListPage(SdzxHelper.gonghuiRecord, "工会活动",
                    IconWidget.getImage(Colors.blue, Icons.category)))));
  }

  void addMenu(int xh, MenuItemViewModel m1, {bool ifYZ = false}) {
    if (ifYZ) {
      if (UIData.CurrentUser!.Position.contains(m1.title)) return;
    }

    if (m1 != null) {
      mList[xh]?.add(m1);
    }
  }

  MenuItemViewModel getMenu(Icon icon1, String title, VoidCallback onTap) {
    if (authority == "all" || authority.contains(title)) {
      return MenuItemViewModel(icon1, title, onTap);
    } else {
      return null as MenuItemViewModel;
    }
  }

  @override
  Widget build(BuildContext context) {
    //getML(context);
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: new AppBar(title: Text("工作记录")),
      floatingActionButton: getFloatingButton(context),
      body: WorkRecordBody(context),
    );
  }

  Widget getFloatingButton(BuildContext context) {
    return new Container();
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    // getML(context);

    list1.add(MenuHelper.getMenuCard(
        "工作记录",
        mList[3]!.map((item) => MenuItem1(data: item)).toList()
            as List<Widget>));
    // list1.add( MenuHelper.getMenuCard("实用工具",mL5.map((item) => MenuItem(data: item)).toList()));
    // list1.add( MenuHelper.getMenuCard("数据查询",mList.map((item) => MenuItem(data: item)).toList()));

    return list1;
  }

  WorkRecordBody(BuildContext context) => SingleChildScrollView(
        child: WorkRecordFields(context),
      );

  WorkRecordFields(BuildContext context) {
    if (!ifLoaded)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    return Container(child: Column(children: getWidgetList(context)));
  }
}
